package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.system.domain.vo.WxMiniRoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.system.mapper.WxMiniRoleMapper;
import com.ruoyi.system.domain.WxMiniRole;
import com.ruoyi.system.service.IWxMiniRoleService;

import javax.validation.constraints.NotEmpty;

/**
 * 微信权限Service业务层处理
 *
 * @author yy
 * @date 2021-11-05
 */
@Service
public class WxMiniRoleServiceImpl implements IWxMiniRoleService {
    @Autowired
    private WxMiniRoleMapper wxMiniRoleMapper;

    /**
     * 查询微信权限
     *
     * @param updateTime 微信权限ID
     * @return 微信权限
     */
    @Override
    public WxMiniRole selectWxMiniRoleById(Date updateTime) {
        return wxMiniRoleMapper.selectWxMiniRoleById(updateTime);
    }

    /**
     * 查询微信权限列表
     *
     * @param wxMiniRole 微信权限
     * @return 微信权限
     */
    @Override
    public List<WxMiniRole> selectWxMiniRoleList(WxMiniRole wxMiniRole) {
        return wxMiniRoleMapper.selectWxMiniRoleList(wxMiniRole);
    }

    /**
     * 新增微信权限
     *
     * @param wxMiniRole 微信权限
     * @return 结果
     */
    @Override
    public int insertWxMiniRole(WxMiniRole wxMiniRole) {
        wxMiniRole.setCreateTime(DateUtils.getNowDate());
        wxMiniRole.setCreateBy(SecurityUtils.getUsername());
        return wxMiniRoleMapper.insertWxMiniRole(wxMiniRole);
    }

    /**
     * 修改微信权限
     *
     * @param wxMiniRole 微信权限
     * @return 结果
     */
    @Override
    public int updateWxMiniRole(WxMiniRole wxMiniRole) {
        wxMiniRole.setUpdateTime(DateUtils.getNowDate());
        wxMiniRole.setUpdateBy(SecurityUtils.getUsername());
        return wxMiniRoleMapper.updateWxMiniRole(wxMiniRole);
    }

    /**
     * 批量删除微信权限
     *
     * @param roleIds 需要删除的微信权限ID
     * @return 结果
     */
    @Override
    public int deleteWxMiniRoleByIds(Long[] roleIds) {
        return wxMiniRoleMapper.deleteWxMiniRoleByIds(roleIds);
    }

    /**
     * 删除微信权限信息
     *
     * @param roleId 微信权限ID
     * @return 结果
     */
    @Override
    public int deleteWxMiniRoleByRolId(Long roleId) {
        return wxMiniRoleMapper.deleteWxMiniRoleById(roleId);
    }

    @Override
    public int saveWxMiniRoleByRolIds(List<WxMiniRole> wxMiniRoleVos) {
        WxMiniRole one= wxMiniRoleVos.get(0);
        if (!StringUtils.isEmpty(one.getRoleId().toString())){
            wxMiniRoleMapper.deleteWxMiniRoleById(one.getRoleId());
            for (WxMiniRole roleItem : wxMiniRoleVos){
                System.out.println(roleItem.toString());
                  wxMiniRoleMapper.insertWxMiniRole(roleItem);
            }
//            for (int i=0;i<wxMiniRoleVos.size();i++){
//                wxMiniRoleMapper.insertWxMiniRole(wxMiniRoleVos.get(i));
//            }
            return 1;
        }else {
            return 0;
        }
    }
}
